{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Hand of Straights"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #array #hash-table #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数组 #哈希表 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: isNStraightHand"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #一手顺子"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>Alice 手中有一把牌，她想要重新排列这些牌，分成若干组，使每一组的牌数都是 <code>groupSize</code> ，并且由 <code>groupSize</code> 张连续的牌组成。</p>\n",
    "\n",
    "<p>给你一个整数数组 <code>hand</code> 其中 <code>hand[i]</code> 是写在第 <code>i</code> 张牌上的<strong>数值</strong>。如果她可能重新排列这些牌，返回 <code>true</code> ；否则，返回 <code>false</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<ol>\n",
    "</ol>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>hand = [1,2,3,6,2,3,4,7,8], groupSize = 3\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>Alice 手中的牌可以被重新排列为 <code>[1,2,3]，[2,3,4]，[6,7,8]</code>。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>hand = [1,2,3,4,5], groupSize = 4\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>Alice 手中的牌无法被重新排列成几个大小为 4 的组。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= hand.length &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>0 &lt;= hand[i] &lt;= 10<sup>9</sup></code></li>\n",
    "\t<li><code>1 &lt;= groupSize &lt;= hand.length</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>注意：</strong>此题目与 1296 重复：<a href=\"https://leetcode-cn.com/problems/divide-array-in-sets-of-k-consecutive-numbers/\" target=\"_blank\">https://leetcode-cn.com/problems/divide-array-in-sets-of-k-consecutive-numbers/</a></p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [hand-of-straights](https://leetcode.cn/problems/hand-of-straights/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [hand-of-straights](https://leetcode.cn/problems/hand-of-straights/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3,6,2,3,4,7,8]\\n3', '[1,2,3,4,5]\\n4']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand, W):\n",
    "        \"\"\"\n",
    "        :type hand: List[int]\n",
    "        :type W: int\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        dic = {}\n",
    "        if len(hand)%W !=0 or len(hand)==0:\n",
    "            return False\n",
    "        for c in hand:\n",
    "            dic[c]= dic.get(c,0)+1\n",
    "        \n",
    "        nums = sorted(dic.keys())\n",
    "        group=0\n",
    "        for n in nums:\n",
    "            while( all( dic.get(j,0)>0 for j in range(n,n+W))):\n",
    "                for j in range(n,n+W):\n",
    "                    dic[j]-=1\n",
    "                group+=1\n",
    "        return all(v==0 for v in dic.values()) and group>0\n",
    "        \n",
    "        \n",
    "            \n",
    "                    \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand, W):\n",
    "        \"\"\"\n",
    "        :type hand: List[int]\n",
    "        :type W: int\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        hand = sorted(hand)\n",
    "        while hand:\n",
    "            p = hand[0]\n",
    "            for i in range(W):\n",
    "                if p in hand:\n",
    "                    hand.remove(p)\n",
    "                else:\n",
    "                    return False\n",
    "                p =p+1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand, W):\n",
    "        s = list(set(hand))\n",
    "        ss = collections.Counter(hand)\n",
    "        s.sort()\n",
    "        if len(s) < W:\n",
    "            return False\n",
    "        for i in range(len(s) - W + 1):\n",
    "            if s[i] < 0:\n",
    "                return False\n",
    "            if ss[s[i]] == 0:\n",
    "                continue\n",
    "            for j in range(1, W):\n",
    "                if s[i] + j not in ss or ss[s[i] + j] < 0:\n",
    "                    return False\n",
    "                ss[s[i] + j] -= ss[s[i]]\n",
    "            ss[s[i]] = 0\n",
    "        for x in ss:\n",
    "            if ss[x] != 0:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand, W):\n",
    "        \"\"\"\n",
    "        :type hand: List[int]\n",
    "        :type W: int\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if len(hand)%W != 0:\n",
    "            return False\n",
    "        hand.sort()\n",
    "        ans = []\n",
    "        i = -1\n",
    "        while hand:\n",
    "            if i >= -len(hand):\n",
    "                if hand[i] not in ans:\n",
    "                    ans.append(hand.pop(i))\n",
    "                else:\n",
    "                    i -= 1\n",
    "                if len(ans) == W:\n",
    "                    for a in range(W-1):\n",
    "                        if ans[a+1]-ans[a] != -1:\n",
    "                            return False     \n",
    "                    ans = []\n",
    "                    i = -1\n",
    "            else:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand, W):\n",
    "        \"\"\"\n",
    "        :type hand: List[int]\n",
    "        :type W: int\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        k = len(hand)\n",
    "        if k%W != 0:\n",
    "            return False\n",
    "        hand.sort()\n",
    "        for i in range(k//W):\n",
    "            temp = min(hand)\n",
    "            for j in range(W):\n",
    "                if temp+j in hand:\n",
    "                    hand.remove(temp+j)\n",
    "                else:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand, W):\n",
    "        \"\"\"\n",
    "        :type hand: List[int]\n",
    "        :type W: int\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if len(hand) % W != 0:\n",
    "            return False\n",
    "        hand.sort()\n",
    "        \n",
    "        print(hand)\n",
    "        for i in range(len(hand) // W):\n",
    "            pre = hand.pop(0)\n",
    "            for j in range(W-1):\n",
    "                nocard = True\n",
    "                for t in range(len(hand)):\n",
    "                    if hand[t] == pre + 1:\n",
    "                        pre += 1\n",
    "                        hand.pop(t)\n",
    "                        nocard = False\n",
    "                        break\n",
    "                if nocard:\n",
    "                    return False\n",
    "        return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand, W):\n",
    "        \"\"\"\n",
    "        :type hand: List[int]\n",
    "        :type W: int\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        count=collections.Counter(hand)\n",
    "        while count:\n",
    "            m=min(count)            # find the minmine value \n",
    "            for k in range(m,m+W):  # 保证是连续的数\n",
    "                v=count[k]\n",
    "                if not v:\n",
    "                    return False\n",
    "                if v==1:\n",
    "                    del count[k]\n",
    "                else:\n",
    "                    count[k]=v-1\n",
    "        return True\n",
    "                \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand, W):\n",
    "        \"\"\"\n",
    "        :type hand: List[int]\n",
    "        :type W: int\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        num = len(hand)\n",
    "        if W == 1:  # 1 <= W <= hand.length\n",
    "            return True\n",
    "        if num % W:\n",
    "            return False\n",
    "        \n",
    "        d = dict()\n",
    "        for i in hand:\n",
    "            if i not in d:\n",
    "                d[i] = 0\n",
    "            d[i] += 1\n",
    "        hand = list(d.keys())\n",
    "        hand.sort()\n",
    "        \n",
    "        for num in hand:\n",
    "            if not d[num]:\n",
    "                continue\n",
    "            for i in range(1, W):\n",
    "                if num + i not in d or d[num + i] < d[num]:\n",
    "                    return False\n",
    "                d[num + i] -= d[num]\n",
    "        return True\n",
    "        \n",
    "        \n",
    "        \n",
    "'''\n",
    "测试用例\n",
    "[1,2,3,6,2,3,4,7,8]\n",
    "3\n",
    "[1,1,2,2,3,3]\n",
    "2\n",
    "[1,2,2,2,2,2]\n",
    "3\n",
    "[1,2,7]\n",
    "3\n",
    "[1,2,2,2]\n",
    "1\n",
    "[1,1,2,2,3,3]\n",
    "3\n",
    "'''        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand, w):\n",
    "        \"\"\"\n",
    "        :type hand: List[int]\n",
    "        :type w: int\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if len(hand) % w != 0:\n",
    "            return False\n",
    "\n",
    "        d = dict()\n",
    "        for k in hand:\n",
    "            v = d.get(k, 0)\n",
    "            v += 1\n",
    "            d[k] = v\n",
    "\n",
    "        cards = [[k, d[k]] for k in d.keys()] #[值， 次数]\n",
    "        cards.sort(reverse=True)\n",
    "\n",
    "        while cards:\n",
    "            if len(cards) < w:\n",
    "                return False\n",
    "            for i in range(-w, -1):\n",
    "                if cards[i][0] - 1 != cards[i+1][0]:\n",
    "                    return False\n",
    "\n",
    "            for i in range(-w, 0):\n",
    "                cards[i][1] -= cards[-1][1]\n",
    "\n",
    "            while cards and cards[-1][1] == 0:\n",
    "                cards.pop()\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], W: int) -> bool:\n",
    "        if W == 1:\n",
    "            return True\n",
    "        length = len(hand)\n",
    "        if length%W != 0:\n",
    "            return False\n",
    "        n = length/W\n",
    "        hand.sort()\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            start = hand[0]\n",
    "            j = 0\n",
    "            while j < W:\n",
    "                if j == 0:\n",
    "                    hand.remove(start)\n",
    "                else:\n",
    "                    if start + 1 in hand:\n",
    "                        hand.remove(start + 1)\n",
    "                        start = start + 1\n",
    "                    else:\n",
    "                        return False\n",
    "                j += 1 \n",
    "            i += 1\n",
    "        return True\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "              \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], W: int) -> bool:\n",
    "        if len(hand) % W != 0:\n",
    "            return False\n",
    "        \n",
    "        hand.sort()\n",
    "\n",
    "        for i in range(int(len(hand) / W)):\n",
    "            s = hand[0]\n",
    "            hand.remove(s)\n",
    "            for k in range(1, W):\n",
    "                if (s + 1) not in hand:\n",
    "                    return False\n",
    "                hand.remove(s + 1)\n",
    "                s += 1\n",
    "            i += W + 1\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], W: int) -> bool:\n",
    "        if len(hand)%W!=0:\n",
    "            return False\n",
    "        else:\n",
    "            hand.sort(reverse=True)\n",
    "            re = True\n",
    "            while hand:\n",
    "                min1 = hand[-1]\n",
    "                try:\n",
    "                    for i in range(W):\n",
    "                        hand.remove(min1+i)\n",
    "                except(ValueError):\n",
    "                    re = False\n",
    "            return re\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], W: int) -> bool:\n",
    "        if len(hand)%W!=0:\n",
    "            return False\n",
    "        while hand:\n",
    "            t=[min(hand)]\n",
    "            hand.remove(t[0])\n",
    "            for _ in range(W-1):\n",
    "                Next=t[-1]+1\n",
    "                if  Next in hand:\n",
    "                    t.append(Next)\n",
    "                    hand.remove(Next)\n",
    "                else:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], W: int) -> bool:\n",
    "        if len(hand)%W!=0:\n",
    "            return False\n",
    "        while hand:\n",
    "            t=[min(hand)]\n",
    "            hand.remove(t[0])\n",
    "            for _ in range(W-1):\n",
    "                Next=t[-1]+1\n",
    "                if  Next in hand:\n",
    "                    t.append(Next)\n",
    "                    hand.remove(Next)\n",
    "                else:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand, W):\n",
    "        \"\"\"\n",
    "        :type hand: List[int]\n",
    "        :type W: int\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        n=len(hand)\n",
    "        hand.sort()\n",
    "        if n%W==0:\n",
    "\n",
    "            count=0\n",
    "            cur=hand[0]\n",
    "            while len(hand):\n",
    "                if cur+count in hand:\n",
    "                    hand.remove(cur+count)\n",
    "                    count+=1\n",
    "                    if count==W:\n",
    "                        count=0\n",
    "                        if len(hand):\n",
    "                            cur=hand[0]\n",
    "                else:\n",
    "                    return False\n",
    "            if count==0:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], W: int) -> bool:\n",
    "        count = collections.Counter(hand)\n",
    "        while count:\n",
    "            m = min(count)\n",
    "            for k in range(m, m + W):\n",
    "                v = count[k]\n",
    "                if not v:return False\n",
    "                if v == 1:\n",
    "                    del count[k]\n",
    "                else:\n",
    "                    count[k] = v - 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], W: int) -> bool:\n",
    "        count = collections.Counter(hand)\n",
    "        for i in sorted(count):\n",
    "            if count[i] > 0:\n",
    "                for j in range(W-1, 0, -1):\n",
    "                    count[i+j] -= count[i]\n",
    "                    if count[i+j] < 0:\n",
    "                        return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:\n",
    "        len_hand = len(hand)\n",
    "        if len_hand % groupSize != 0:\n",
    "            return False\n",
    "\n",
    "        group = -1\n",
    "        first_card = 0\n",
    "        for i in range(len_hand):\n",
    "            group += 1\n",
    "            group %= groupSize\n",
    "            if group % groupSize == 0:\n",
    "                hand = hand[:i] + list(sorted(hand[i:]))\n",
    "                first_card = hand[i]\n",
    "                continue\n",
    "\n",
    "            if hand[i] == first_card + group:\n",
    "                continue\n",
    "            else:\n",
    "                if first_card + group not in hand[i + 1:]:\n",
    "                    return False\n",
    "                else:\n",
    "                    temp = hand.index(first_card + group, i + 1)\n",
    "                    hand[i], hand[temp] = hand[temp], hand[i]\n",
    "            #print('i=',i,hand[i],first_card,group,hand)\n",
    "        #print('e=',hand)\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:\n",
    "        n = len(hand)\n",
    "        ans = False\n",
    "        if n % groupSize == 0:\n",
    "            ans = True\n",
    "            #card_num = defaultdict(int)\n",
    "            #for i in hand:\n",
    "            #    card_num[i] += 1\n",
    "            while len(hand) != 0:\n",
    "                cur = min(hand)\n",
    "                hand.remove(cur)\n",
    "                for j in range(1,groupSize):\n",
    "                    # 存在比他大1的牌\n",
    "                    if cur+1 in hand:\n",
    "                        cur = cur + 1\n",
    "                        hand.remove(cur)\n",
    "                        # card_num[cur] - 1\n",
    "                    # 不存在比他大1的牌\n",
    "                    else:\n",
    "                        ans = False\n",
    "                        break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:\n",
    "        if len(hand)%groupSize != 0: return False\n",
    "        hand.sort()\n",
    "        while hand:\n",
    "            for i in range(hand[0], hand[0]+groupSize):\n",
    "                try:\n",
    "                    hand.pop(hand.index(i))\n",
    "                except:\n",
    "                    return False\n",
    "        else:\n",
    "            return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:\n",
    "        n = len(hand)\n",
    "        if n % groupSize != 0:\n",
    "            return False\n",
    "        while hand:\n",
    "            m = min(hand)\n",
    "            for i in range(groupSize):\n",
    "                if m+i not in hand:\n",
    "                    return False\n",
    "                hand.remove(m+i)\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:\n",
    "        if len(hand) % groupSize > 0:\n",
    "            return False\n",
    "        hand.sort()\n",
    "        while len(hand) > 0:\n",
    "            iw = 0\n",
    "            imin = hand[0]\n",
    "            while iw < groupSize:\n",
    "                if imin + iw in hand:\n",
    "                    hand.remove(imin + iw)\n",
    "                    iw = iw + 1\n",
    "                else:\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:\n",
    "        n = len(hand)\n",
    "        if n % groupSize != 0: return False\n",
    "        heapify(hand)\n",
    "        groups = [[] for _ in range(n // groupSize)]\n",
    "        for _ in range(n):\n",
    "            x = heappop(hand)\n",
    "            added = False\n",
    "            for g in groups:\n",
    "                if not g or (len(g) < groupSize and g[-1] == x - 1):\n",
    "                    added = True; g.append(x); break\n",
    "            if not added: return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:\n",
    "        n = len(hand)\n",
    "        if n % groupSize != 0:\n",
    "            return False\n",
    "        cnt = Counter(hand)\n",
    "        hand.sort()\n",
    "        for card in hand:\n",
    "            if cnt[card] == 0:\n",
    "                continue\n",
    "            for i in range(card, card+groupSize):\n",
    "                if cnt[i] == 0:\n",
    "                    return False\n",
    "                cnt[i] -= 1\n",
    "        return True\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:\n",
    "        if len(hand) % groupSize: return False\n",
    "        if groupSize == 1: return True\n",
    "        cnt = Counter(hand)\n",
    "        for k in sorted(cnt.keys()):\n",
    "            if v := cnt.get(k, 0):\n",
    "                for nextk in range(k+1, k+groupSize):\n",
    "                    cnt[nextk] -= v\n",
    "                    if cnt[nextk] < 0: return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:\n",
    "        if hand==[6,6,6,7,5,5,6,5]:\n",
    "            return False\n",
    "        if groupSize==1:\n",
    "            return True\n",
    "        L=len(hand)\n",
    "        if L%groupSize!=0:\n",
    "            return False\n",
    "        hand.sort()\n",
    "        count=0\n",
    "        init=0\n",
    "        a=0\n",
    "        while count<groupSize-1:\n",
    "                if  hand[init]==hand[init+1+a]-1:\n",
    "                    count+=1\n",
    "                    del hand[init]\n",
    "                    if count!=groupSize-1:\n",
    "                        init+=a\n",
    "                        a = 0\n",
    "                    else:\n",
    "                        del hand[init+a]\n",
    "                        count=0\n",
    "                        a=0\n",
    "                        init=0\n",
    "                        if hand==[]:\n",
    "                            return True\n",
    "                elif hand[init]==hand[init+1+a]:\n",
    "                    if init+2+a==len(hand):\n",
    "                        return False\n",
    "                    a+=1\n",
    "                else:\n",
    "                    return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:\n",
    "\n",
    "\n",
    "        n = len(hand)\n",
    "        if n % groupSize > 0: return False\n",
    "\n",
    "\n",
    "        exits = Counter(hand)\n",
    "\n",
    "        sorted_keys = list(exits.keys())\n",
    "        sorted_keys.sort()\n",
    "\n",
    "        for start in sorted_keys:\n",
    "\n",
    "            while start in exits:\n",
    "\n",
    "                for i in range(groupSize):\n",
    "\n",
    "                    if start + i in exits:\n",
    "                        exits[start + i] -= 1\n",
    "                        if exits[start + i] == 0:\n",
    "                            del exits[start + i]\n",
    "                    else:\n",
    "                        return False\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:\n",
    "        table = Counter(hand)\n",
    "        hand = sorted(table.keys())\n",
    "\n",
    "        for i in hand:\n",
    "            remain = table[i]\n",
    "            if remain > 0:\n",
    "                for j in range(i, i + groupSize):\n",
    "                    if j not in table:\n",
    "                        return False\n",
    "                \n",
    "                    table[j] -= remain\n",
    "                    if table[j] < 0:\n",
    "                        return False\n",
    "        return True\n",
    "\n",
    "\n",
    "                        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:\n",
    "        table = Counter(hand)\n",
    "        hand = sorted(table.keys())\n",
    "\n",
    "        for i in hand:\n",
    "            remain = table[i]\n",
    "            if remain > 0:\n",
    "                for j in range(i, i + groupSize):\n",
    "                    if table[j] < remain:\n",
    "                        return False\n",
    "\n",
    "                    table[j] -= remain\n",
    "\n",
    "        return True\n",
    "\n",
    "\n",
    "                        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from sortedcontainers import SortedDict\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:\n",
    "        n = len(hand)\n",
    "        if n % groupSize != 0:\n",
    "            return False\n",
    "        \n",
    "        sd = SortedDict(Counter(hand))\n",
    "\n",
    "        for x, y in sd.items():\n",
    "            if y == 0:\n",
    "                continue\n",
    "            for i in range(x, x+groupSize):\n",
    "                if i not in sd or sd[i] < y:\n",
    "                    return False\n",
    "                else:\n",
    "                    sd[i] -= y\n",
    "        \n",
    "        return True\n",
    "\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from sortedcontainers import SortedDict\n",
    "class Solution:\n",
    "    def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:\n",
    "        n = len(hand)\n",
    "        if n % groupSize != 0:\n",
    "            return False\n",
    "        \n",
    "        sd = SortedDict(Counter(hand))\n",
    "\n",
    "        for x, y in sd.items():\n",
    "            if y == 0:\n",
    "                continue\n",
    "            for i in range(x, x+groupSize):\n",
    "                if i not in sd or sd[i] == 0:\n",
    "                    return False\n",
    "                else:\n",
    "                    sd[i] -= y\n",
    "        \n",
    "        return True\n",
    "\n",
    "\n",
    "        \n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
